package com.pic.mgt.syushin;

import java.io.File;
import java.io.IOException;

import com.pic.mgt.log.IgxLog;
import com.pic.mgt.monitor.BusinessUnitFactory;
import com.pic.mgt.monitor.RedundantConfirmationFileException;
import com.pic.mgt.monitor.UnitConverter;
import com.pic.mgt.monitor.WrongUnitFormatException;
import com.pic.mgt.syushin.converter.SyushinBusinessUnitConverter;
import com.pic.mgt.syushin.unit.BatchSyushinBusinessUnit;
import com.pic.mgt.syushin.unit.RealSyushinBusinessUnit;
import com.pic.mgt.unit.Unit;

public class SyushinBusinessUnitFactory extends BusinessUnitFactory {
	private UnitConverter realSyushinBusinessUnitConverter;
	private UnitConverter batchSyushinBusinessUnitConverter;
	
	public SyushinBusinessUnitFactory(String appintedHaishinTypeStr,
			  						  String generalHaishinTypeStr,
			  						  String realSyushinTypeStr,
			  						  String batchSyushinTypeStr) {
		super(appintedHaishinTypeStr,
			  generalHaishinTypeStr,
			  realSyushinTypeStr,
			  batchSyushinTypeStr);
	}

	@Override
	public Unit createUnit(File confirmationFile)
									throws IOException,
										   WrongUnitFormatException,
										   RedundantConfirmationFileException {
		Unit syushinBusinessUnit = null;
		
		try {
			String syushinTypeString = (String)
						super.businessTypeParser.parse(
										confirmationFile.getName());
			// 即時處理配信
			if (syushinTypeString.equals(REAL_SYUSHIN)) {
				// ********************** Log **********************
				if (IgxLog.messageDetailsLog.isInfoMode()) {
					IgxLog.messageDetailsLog
							.info(this, "Creating real syushin business unit for confirmation file '{}'",
									confirmationFile.getName());
				}
				// ********************** Log **********************
				
				/*
				 *  throws NoMatchHaishinTypeException,
				 *         IOException,
				 *         WrongConfirmationContentFormatException,
				 *         WrongUnitFormatException 
				 */
				syushinBusinessUnit =
					this.realSyushinBusinessUnitConverter
						.parseToUnit(confirmationFile,
										 new RealSyushinBusinessUnit(REAL_SYUSHIN));
				
				// ********************** Log **********************
				if (IgxLog.messageDetailsLog.isInfoMode()) {
					IgxLog.messageDetailsLog
							.info(this, "Creating real syushin business unit for confirmation file '{}' completed.",
									confirmationFile.getName());
				}
				// ********************** Log **********************
			}
			// 批次處理配信
			else if (syushinTypeString.equals(BATCH_SYUSHIN)) {
				// ********************** Log **********************
				if (IgxLog.messageDetailsLog.isInfoMode()) {
					IgxLog.messageDetailsLog
							.info(this, "Creating batch syushin business unit for confirmation file '{}'",
									confirmationFile.getName());
				}
				// ********************** Log **********************
				
				/*
				 *  throws NoMatchHaishinTypeException,
				 *         IOException,
				 *         WrongConfirmationContentFormatException,
				 *         WrongUnitFormatException 
				 */
				syushinBusinessUnit = 
					this.batchSyushinBusinessUnitConverter
						.parseToUnit(confirmationFile,
								new BatchSyushinBusinessUnit(BATCH_SYUSHIN));
				
				// ********************** Log **********************
				if (IgxLog.messageDetailsLog.isInfoMode()) {
					IgxLog.messageDetailsLog
							.info(this, "Creating batch syushin business unit for confirmation file '{}' completed.",
									confirmationFile.getName());
				}
				// ********************** Log **********************
			}
			else {
				// 找不到對應的業務'型態'
				throw new WrongUnitFormatException(
						"Can't find match syushin type '"
								+ syushinTypeString
								+ "' to create haishin business unit for confirmation file '"
								+ confirmationFile.getName() + "'.");
			}
		} catch (WrongUnitFormatException e1) {
			// ********************** Log **********************
			IgxLog.messageDetailsLog.error(this, e1.getMessage());
			// ********************** Log **********************
			
			throw e1;
		} catch (IOException e2) {
			// ********************** Log **********************
			if (IgxLog.messageDetailsLog.isInfoMode()) {
				IgxLog.messageDetailsLog.debug(this, e2.getMessage());
			}
			// ********************** Log **********************
			
			throw e2;
		}
		
		return syushinBusinessUnit;
	}
	
	/**
	 * 判斷是否為有效的即信業務型態
	 * @param syushinBusinessTypeStr
	 * @return
	 */
	public static boolean isEffectiveSyushinBusinessType(String syushinBusinessTypeStr) {
		if (syushinBusinessTypeStr.equals(REAL_SYUSHIN)||
			syushinBusinessTypeStr.equals(BATCH_SYUSHIN)) {
			return true;
		}
		else {
			return false;
		}
	}

	// --- getter and setter ---
	
	public void setRealSyushinBusinessUnitConverter(
			SyushinBusinessUnitConverter realSyushinBusinessUnitConverter) {
		this.realSyushinBusinessUnitConverter = realSyushinBusinessUnitConverter;
	}

	public void setBatchSyushinBusinessUnitConverter(
			SyushinBusinessUnitConverter batchSyushinBusinessUnitConverter) {
		this.batchSyushinBusinessUnitConverter = batchSyushinBusinessUnitConverter;
	}

}
